<!-- 
@name: Login.vue 
@description: 登录 
@route: /login
@author: sx
@date: 2020/09/01
-->
<template >
	<div @click="registerGift" style="position: relative;">
		<img src="../assets/images/wawa/1.png" class="img1" mode='widthFix' alt="">
		<img src="../assets/images/wawa/2.gif" class="img2" alt="">
	</div>
</template>
<script>
import utils from '@/assets/js/utils';
import qs from 'qs';
import Vue from 'vue';

export default {
	data() {
		return {
			idcard: '',
			phone: '',
			code: '',
			time: 0,
			role: 'member', //控制商家 merchant 或 用户登录 member 的变量
			wxUserInfo: {}, //微信用户信息
			show: false,
			agreement: '',
		};
	},
	mounted() {
		if (this.$route.query.role) this.role = this.$route.query.role;
	},
	methods: {
		get_member() {
			var params = {};
			this.$myApi.get_member(params).then((res) => {
				if (res.code == 0) {
					this.$store.dispatch('SET_USERINFO', res.result);
				} else {
				}
			});
		},

		/**
		 * 验证码倒计时结束事件
		 */
		checkTime(e) {
			this.time = 0;
		},
		/**
		 * 切换登录
		 */
		handleLoginChange(val) {
			(this.phone = ''), (this.idcard = ''), (this.code = '');
			this.role == val;
		},

		/**
		 *
		 */
		registerGift() {
			if (!this.$store.state.subscribe) {
				this.$toast.fail('请先关注公众号');
				return;
			}
			if (this.$store.state.registeGift) {
				this.$toast.fail('您已领取过该礼包');
				return;
			}
			var params = {};
			// console.log('商户参数为', params);
			this.$myApi.registerGift(params).then((res) => {
				if (res.status == '00000') {
					this.$toast.success('领取成功');
				}
			});
		},
		/**
		 * 提交登录
		 */
		handleLogin() {
			if (!this.checkLoginFrom(!0)) {
				return;
			}
			let params,
				wxUserInfo = this.wxUserInfo;
			localStorage.setItem('role', this.role);
			// console.log(wxUserInfo)
			if (this.role == 'merchant') {
				params = {
					phone: this.phone,
					code: this.code,
					openid: this.$store.state.openid,
				};
				// console.log('商户参数为', params);
				this.$myApi.login_vendor(params).then((res) => {
					if (res.code == 0) {
						this.$toast('登录成功');
						this.$router.replace('/TheVendor/home');
					}
				});
			} else if (this.role == 'member') {
				params = {
					idcard: this.idcard,
					phone: this.phone,
					code: this.code,
					referrer: this.$route.query.pkey,
					// openid: this.$store.state.openid,
					// headimgurl: wxUserInfo.headimgurl,
					// nickname: wxUserInfo.nickname,
					// name: '测试名称',
				};

				// console.log('用户参数为', params);

				this.$myApi.bind_member(params).then((res) => {
					if (res.code == 0) {
						if (res.codeMsg) {
							this.$dialog
								.confirm({
									title: '提示',
									confirmButtonText: '确定',
									cancelButtonText: '取消',
									message: res.codeMsg,
								})
								.then(() => {
									this.$router.back();
									// this.$router.push('/personal/home');
								})
								.catch(() => {
									this.$router.back();
									// this.$router.push('/personal/home');
									// router.back()
									// on cancel
								});
						} else {
							this.$toast('绑定成功');
							this.$router.back();
						}
						// this.$store.dispatch("SET_USERINFO", res);
						// this.$store.dispatch('SET_WXUSERINFO', this.wxUserInfo);
						// this.loadPonits(res)
					}
				});
			}
		},

		/**
		 * 发送验证码
		 */
		handleGetcode() {
			if (!this.checkLoginFrom(!1)) {
				return;
			}
			let params = {
				phone: this.phone,
				flag: true,
			};
			if (this.role == 'member') {
				params = {
					idcard: this.idcard,
					phone: this.phone,
					flag: true,
				};
				this.$myApi.captcha_member(params).then((res) => {
					if (res.code == 0) {
						this.time = 60000;
						this.$toast('获取成功，请查收');
					}
				});
			} else if (this.role == 'merchant') {
				params = {
					phone: this.phone,
				};
				this.$myApi.captcha_vendor(params).then((res) => {
					if (res.code == 0) {
						this.time = 60000;
						this.$toast('获取成功，请查收');
					}
				});
			}
		},
		/**
		 * 检查表单
		 */
		checkLoginFrom(type) {
			let result = !0;
			if (this.role == 'member') {
				if (!this.idcard) {
					this.$toast('请先填写身份证号');
					this.$refs.idcardInput.focus();
					return (result = !1);
				}
				// else if (!utils.checkIdCard(this.idcard)) {
				//   this.$toast('请填写正确的身份证号');
				//   this.$refs.idcardInput.focus()
				//   return result = !1
				// }
			}
			if (!this.phone) {
				this.$toast('请先填写手机号');
				this.$refs.phoneInput.focus();
				return (result = !1);
			}
			// else if (!utils.checkMobile(this.phone)) {
			//   this.$toast('请填写正确的手机号');
			//   this.$refs.phoneInput.focus()
			//   return result = !1
			// }
			if (!type) return result;
			if (!this.code) {
				this.$toast('请填写验证码');
				this.$refs.codeInput.focus();
				return (result = !1);
			}
			return (result = !0);
		},
	},
};
</script>
<style lang="less" scoped>
.img1 {
	width: 7.5rem;
}
.img2 {
	width: 7.5rem;
	position: absolute;
	left: -0.4rem;
	/* right: 0px; */
	bottom: 1rem;
}
</style>